package com.seeyon.apps.ext.qzwlSSO.controller;

import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.seeyon.ctp.common.affair.manager.AffairManager;
import com.seeyon.ctp.common.controller.BaseController;
import com.seeyon.ctp.common.po.affair.CtpAffair;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.util.annotation.NeedlessCheckLogin;

public class WLSSOController extends BaseController {
	private AffairManager affairManager;
	private OrgManager orgManager;
	/**
	 * 通过Summid 进行数据跳转
	 */
//	private String OA_SERVER_URL="http://47.104.151.11:443";//oa服务器地址；记得切换测试/正式
	private String OA_SERVER_URL="http://120.37.121.214:443";//oa服务器地址；记得切换测试/正式
	//demo url=http://A8地址/seeyon/ncsso/login.do?ticket=**&summaryId=**
	@NeedlessCheckLogin
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		String ticket = request.getParameter("ticket");
        String summaryId = request.getParameter("summaryId");
		try {
			    List<CtpAffair>  affairList=affairManager.getAffairs(Long.valueOf(summaryId));
			    CtpAffair ctpAffair=affairList.get(0);//获取一条待办
			    Long affairId=ctpAffair.getId();
			    V3xOrgMember member=this.orgManager.getMemberById(ctpAffair.getMemberId());//获取用户数据
//			    String userid=ticket.split("NCSSO")[0];
			    String userid=member.getLoginName()+"NCSSO"+System.currentTimeMillis();
			    String ssoRedirect = OA_SERVER_URL + "/seeyon/login/sso?ticket=" + userid + "&from=ncsso";
			    String tourl="/seeyon/collaboration/collaboration.do?method=summary&openFrom=listPending&affairId="+affairId;
				String tourlEncodee = URLEncoder.encode(tourl, "UTF-8");
				ssoRedirect = ssoRedirect + "&tourl=" + tourlEncodee;
				System.out.println("ssoRedirect-1:" + ssoRedirect);
				response.sendRedirect(ssoRedirect);
		} catch (Exception e) {
			response.getWriter().println("请检查登录账户是否存在,流程是否正常!");
		}
		return null;
	}
	public void setAffairManager(AffairManager affairManager) {
		this.affairManager = affairManager;
	}

	public void setOrgManager(OrgManager orgManager) {
		this.orgManager = orgManager;
	}
}